<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

use App\Models\InfoCategory;
use App\Models\Navigation;
use App\Services\DictionaryService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

/**
 * 导航表单验证
 * Class NavigationRequest
 * @package App\Http\Requests
 */
class NavigationRequest extends BaseRequest
{

    /**
     * 注入表单请求验证时自动校验
     *
     * @var bool
     */
    public $autoValidate = false;

    /**
     * 当前经过身份验证的用户是否可以执行请求操作
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => 'required|integer',
            'created_user_id' => 'required|integer',
            'type' => [
                'required',
                'integer',
                Rule::in(DictionaryService::getItemsValuesArray('navigationTypeVar')),
            ],
            'info_category_id' => [
                'required_if:type,1',
                'integer',
                function ($attribute, $value, $fail) {
                    $info_category_ids = InfoCategory::pluck('id')->toArray();
                    if ($this->type == 1 && !in_array($value, $info_category_ids)) {
                        $fail(__('navigation.error_info_category_id'));
                    }
                }
            ],
            'parent_id' => [
                'nullable',
                'integer',
                function ($attribute, $value, $fail) {
                    $this->validateParent($attribute, $value, $fail);
                }
            ],
            'name' => "required|min:2|max:50",
            'id_name' => ['nullable', 'min:2', 'max:100', 'regex:/^[a-zA-Z]+$/', Rule::unique('navigation')->ignore($this->navigation)->where(function ($query) {
                return $query->where('deleted_at', null);
            })],
            'href' => 'nullable|max:100',
            'href_target' => 'nullable|max:100',
            'rel' => 'nullable|max:20',
            'sort' => 'required|integer',
            'status' => 'required|boolean',
            'sort.*.id' => 'required|integer',
            'sort.*.sort' => 'required|integer',
        ];
    }

    /**
     * 定义场景
     * @var \string[][]
     */
    public $scenes = [
        'store' => [
            'created_user_id',
            'type',
            'info_category_id',
            'parent_id',
            'name',
            'id_name',
            'href',
            'href_target',
            'rel',
            'status',
        ],
        'update' => [
            'type',
            'info_category_id',
            'parent_id',
            'name',
            'id_name',
            'href',
            'href_target',
            'rel',
            'status',
        ],
        'destroy' => ['id'],
        'updateStatus' => ['status'],
        'updateSort' => ['sort.*.id', 'sort.*.sort']
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => __('common.id'),
            'type' => __('navigation.type'),
            'info_category_id' => __('navigation.info_category_id'),
            'parent_id' => __('navigation.parent_id'),
            'name' => __('navigation.name'),
            'id_name' => __('navigation.id_name'),
            'href' => __('common.href'),
            'href_target' => __('common.href_target'),
            'rel' => __('common.rel'),
            'status' => __('common.status'),
            'sort.*.id' => __('common.id'),
            'sort.*.sort' => __('common.sort'),
        ];
    }

    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->offsetSet('created_user_id', Auth::id());
        if (!isset($this->parent_id) || empty($this->parent_id)) $this->offsetSet('parent_id', 0);
        if (isset($this->type) && $this->type == 2) $this->offsetSet('info_category_id', 0);
    }


    /**
     * 判断上一级导航合法性
     * @param $attribute
     * @param $value
     * @param $fail
     */
    public function validateParent($attribute, $value, &$fail)
    {
        if (!empty($value)) {
            $parent = Navigation::find($value);
            if (!$parent) {
                $fail(__('navigation.error_parent_id'));
            }
            if (isset($this->id) && $this->id == $value) {
                $fail(__('navigation.error_parent_id'));
            }
        }
    }


}


